<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="loginFormRef" :rules="loginFormRules" :model="loginForm">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user"
            v-model="loginForm.username"
            placeholder="用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            v-model="loginForm.password"
            placeholder="密码"
            show-password
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="primary" @click="resetLoginForm" plain
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "长度在 3 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.$refs.loginFormRef.validate((valid) => {
        if (!valid) return;
        this.$http.post("login", this.loginForm).then((result) => {
          if (result.data.meta.status !== 200)
            return this.$message.error("登录失败！");
          this.$message.success("恭喜你，登录成功！");
          // 1.将登录成功之后的token保存到客户端的sessionStorage,
          console.log(result.data.data);
          sessionStorage.setItem("token", result.data.data.token);
          this.$router.push("/home");
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-color: #2b4b6b;
  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 8px;
    .avatar_box {
      height: 100px;
      width: 100px;
      padding: 5px;
      margin: 0 auto;
      transform: translateY(-50%);
      border: 3px solid rgb(180, 180, 180);
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      background-color: #fff;
      img {
        width: 100%;
        border-radius: 50%;
        transform: scale(0.9);
        background-color: #eee;
      }
    }
    .el-form {
      display: flex;
      flex-wrap: wrap;
      justify-content: center;
      align-items: center;
      .el-input {
        width: 260px;
      }
      .el-button {
        margin: 0 20px;
      }
    }
  }
}
</style>

